//
//  BHMeOrderChildViewController.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/8/27.
//

import UIKit

class BHMeOrderChildViewController: BHBaseViewController {

    
    lazy var tableView: UITableView = {
        let view = UITableView.buildTableView(style: .plain)
        view.regiseterNib(nibName: "BHMyOrderListTableViewCell")
        view.delegate = self
        view.dataSource = self
        view.isHidden = false
        return view
    }()
    
    
    var childType: Int = 0
    fileprivate var orderArray: [BHMeUserOrderInfoModel] = []
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = .color_f8f8f8

        self.view.addSubview(self.tableView)
        
        self.tableView.addRefresh {[weak self] in
            self?.page = 1
            self?.queryOrderService()
        }
        self.tableView.addloadMore {[weak self] in
            self?.page += 1
            self?.queryOrderService()
            
        }
       
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        self.tableView.frame =   CGRect(x: 0, y: 0, width: UIScreen.width, height: self.view.bh_height)
    }
    
    
    func refresh()  {
        self.page = 1
        self.queryOrderService()
    }
    
    func loadOrderService() {
        if self.orderArray.count == 0 {
            self.refresh()
        }
    }
   
}


extension BHMeOrderChildViewController: UITableViewDelegate,UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.orderArray.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return BHMyOrderListTableViewCell.cellHeight
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView.init(frame: CGRect(x: 0, y: 0, width: UIScreen.width, height: 10))
        view.backgroundColor = .color_f8f8f8
        return view
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "BHMyOrderListTableViewCell", for: indexPath) as! BHMyOrderListTableViewCell
        let model = self.orderArray[indexPath.section]
        cell.model = model
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//        BHToast.toast(text: "功能开发中，敬请期待")
        let model = self.orderArray[indexPath.section]
        BHRouter.shared.bh_goOrderDetail(orderId: model.orderId)
    }
}

extension BHMeOrderChildViewController{
    func queryOrderService()  {
        let manager = BHHTTPClient.init()
        manager.get(url: String.order_myOrder_url, paramters: [:]) {[unowned self] (success, errorMsg, responseDic) in
            self.tableView.endRefresh()
            self.tableView.endLoading()
            if success == true {
                let result = BHJsonTool.decodeDicFromDic(dic: responseDic, key: "result")
                let total = BHJsonTool.decodeIntFromDic(dic: result, key: "total")
                if self.page == 1 {
                    self.tableView.endLoadWithNoMoreData(false)
                    if self.orderArray.count != 0 {
                        self.orderArray.removeAll()
                    }
                }
                
                let records = BHJsonTool.decodeArrayFromDic(dic: result, key: "records")
                for item in records {
                    let model = BHMeUserOrderInfoModel.init()
                    model.parseDic(item as! [String:Any])
                    self.orderArray.append(model)
                }
                
                self.tableView.reloadData()
                if self.orderArray.count >= total {
                    self.tableView.endLoadWithNoMoreData(true  )
                }
                
                
            }else {
                BHToast.toast(text: errorMsg)
                if self.page > 1 {
                    self.page -= 1
                }
            }
        }
    }
}
